package com.lagou.controller;


import com.github.pagehelper.PageInfo;
import com.lagou.domain.PromotionAd;
import com.lagou.domain.PromotionAdVO;
import com.lagou.domain.ResponseResult;
import com.lagou.service.PromotionAdService;
import com.lagou.utils.ImageUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/PromotionAd")
public class PromotionAdController {

    @Autowired
    private PromotionAdService promotionAdService;

    /*广告分页查询*/
    @RequestMapping("/findAllPromotionAdByPage")
    public ResponseResult findAllPromotionAdByPage(PromotionAdVO adVo) {
        //pageInfo中储存这分页的总页数、当页数等
        PageInfo<PromotionAd> allPromotionAdByPage = promotionAdService.findAllPromotionAdByPage(adVo);
        ResponseResult responseResult = new ResponseResult(true, 200, "广告分页数据查询成功", allPromotionAdByPage);
        return responseResult;
    }

    /*广告图片上传*/
    @RequestMapping("/PromotionAdUpload")
    public ResponseResult promotionAdUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        ImageUploadUtils imageUploadUtils = new ImageUploadUtils();
        HashMap<String, String> map = imageUploadUtils.ImageUpload(file, request);
        ResponseResult responseResult = new ResponseResult(true, 200, "广告图片上传成功", map);
        return responseResult;
    }

    /*
    新增或更新广告位置
    */
    @RequestMapping("/saveOrUpdatePromotionAd")
    public ResponseResult saveOrUpdatePromotionAd(@RequestBody PromotionAd promotionAd) {
        System.out.println(promotionAd);
        ResponseResult result = null;
        if (promotionAd.getId() == null) {
            Date date = new Date();
            promotionAd.setCreateTime(date);
            promotionAd.setUpdateTime(date);
            promotionAdService.savePromotionAd(promotionAd);
            result = new ResponseResult(true, 200, "新增广告成功", null);
        } else {
            Date date = new Date();
            promotionAd.setUpdateTime(date);
            promotionAdService.updatePromotionAd(promotionAd);
            result = new ResponseResult(true, 200, "修改广告成功", null);
        }
        return result;

    }

    /**
     * 根据id回显 广告数据
     */
    @RequestMapping("/findPromotionAdById")
    public ResponseResult findPromotionAdById(@RequestParam int id) {
        try {
            PromotionAd promotionAd = promotionAdService.findPromotionAdById(id);
            ResponseResult result = new ResponseResult(true, 200, "回显广告信息成功", promotionAd);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*点击按钮，实现状态的动态上下线*/
    @RequestMapping("/updatePromotionAdStatus")
    public ResponseResult updateCourseStatus(@RequestParam Integer id, @RequestParam Integer status) {
        //执行修改操作
        if (status == 1) {
            promotionAdService.updatePromotionAdStatus(id, status);
        } else {
            promotionAdService.updatePromotionAdStatus(id, 0);
        }
        HashMap<Object, Object> map = new HashMap<>();
        map.put("status", status);
        ResponseResult responseResult = new ResponseResult(true, 200, "状态修改成功", map);
        return responseResult;
    }


}
